﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DLL;

namespace BLL
{
    public static class gg_BLL
    {
        //创建数据对象
        private static dbExamDataContext db = new dbExamDataContext();

        /// <summary>
        /// 获取公告数据源
        /// </summary>
        public static object GetGGSource()
        {
            object obj = new object();
            obj = from p in db.GG orderby p.ggDate descending select p;
            return obj;
        }

        /// <summary>
        /// 添加新公告
        /// </summary>
        /// <param name="newGG">新公告数据对象</param>
        public static bool AddNewGG(GG newGG)
        {
            bool bl = false;
            try
            {
                //验证相同数据是否已存在
                if (db.GG.Count(p=>p.ggTitle==newGG.ggTitle)>0)
                {
                    return false;
                }
            }
            catch { }
            //准备数据
            db.GG.InsertOnSubmit(newGG);
            //提交数据
            db.SubmitChanges();
            bl = true;
            return bl;
        }
        /// <summary>
        /// 删除公告
        /// </summary>
        /// <param name="ggNum">要删除的公告的编号</param>
        public static bool DeleteGG(string ggNum)
        {
            bool bl = false;
            db = new dbExamDataContext();
            try
            {
                //找到要删除的数据
                GG gg = db.GG.Single(p => p.ggNum == decimal.Parse(ggNum));
                //准备数据
                db.GG.DeleteOnSubmit(gg);
                //执行删除
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }

        /// <summary>
        /// 更新公告
        /// </summary>
        /// <param name="ggNum">要更新的公告编号</param>
        /// <param name="newGG">新的数据内容</param>
        public static bool UpdateGG(string ggNum, GG newGG)
        {
            bool bl = false;
            try
            {
                //找到要更新的数据
                GG gg = db.GG.Single(p => p.ggNum == decimal.Parse(ggNum));
                //准备数据
                gg.ggTitle = newGG.ggTitle;
                gg.ggDate = newGG.ggDate;
                gg.ggCont = newGG.ggCont;
                //执行更新
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }
    }
}
